<?php
class GuestbookAction extends Action {
    
    //留言本添加流程
    //用途：POST
    public function add(){
    	
    	$data["real_name"] = $this->_post("real_name");
    	$data["telephone"] = $this->_post("telephone");
    	$data["email"] = $this->_post("email");
    	$data["qq"] = $this->_post("qq");
    	$data["content"] = $this->_post("content");
    	$data["catalog_id"] = $this->_get("catalog_id");
    	$data["timeline"] = time();
    	$data["verifycode"] = $this->_post("verifycode");
    	
    	if(!ValidateUtil::isChineseName($data["real_name"])){
    		echo CommonUtil::alertAndGoBack("请填写正确的姓名！");
			return;
    	}
    	if(!ValidateUtil::isCellphone($data["telephone"]) && !ValidateUtil::isTelephone($data["telephone"])){
    		echo CommonUtil::alertAndGoBack("请填写正确的联系电话！");
			return;
    	}
    	if(!ValidateUtil::isEmail($data["email"])){
    		echo CommonUtil::alertAndGoBack("请填写正确的EMAIL！");
			return;
    	}
    	if(empty($data["content"])){
    		echo CommonUtil::alertAndGoBack("留言内容不能为空！");
			return;
    	}
    	if($data["verifycode"] != session("verifycode")){ 
		   echo CommonUtil::alertAndGoBack("验证码错误！");
		   return;
		}
		
		$result = GuestBookHelper::add($data);
		if($result === false){
			echo CommonUtil::alertAndGoBack("服务器繁忙，请稍后重试！");
			return;
		}
		//执行成功
		$catalog = CatalogHelper::getObject("catalog_id={$data["catalog_id"]}");
		$url = __APP__."/t/".$catalog["catalog_url_name"];
		
		header("Content-Type: text/html; charset=utf-8");
		echo '<script language="javascript">alert("操作成功，等待官方审核！");window.location.href="'.$url.'";</script>';
    	
    }
    
    
}